package com.rs.game.player.content.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.rs.game.Animation;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;

public class Furniture {
	
	private static House house;
	
	public Furniture(House house) {
		Furniture.house = house;
	}
	
	public static enum FurnitureObject {
		/* Parlour */
		Crude_chair("Chair space", 13581, 1, 66, new Item(960, 2)),
		Wooden_chair("Chair space", 13582, 8, 96, new Item(960, 3)),
		Rocking_chair("Chair space", 13583, 14, 99, new Item(960, 3)),
		Oak_chair("Chair space", 13584, 19, 120, new Item(8778, 2)),
		Oak_armchair("Chair space", 13585, 26, 180, new Item(8778, 3)),
		Teak_armchair("Chair space", 13586, 35, 180, new Item(8780, 2)),
		Mahogany_armchair("Chair space", 13587, 50, 280, new Item(8782, 2)),
		/* Garden */
		Exit_portal("Centrepiece space", 13405, 1, 100, new Item(2351, 10)),
		Decorative_rock("Centrepiece space", 13406, 5, 100, new Item(3420, 5)),
		Pond("Centrepiece space", 13407, 10, 100, new Item(1761, 10)),
		Imp_statue("Centrepiece space", 13408, 15, 150, new Item(3420, 5), new Item(1761, 5)),
		Dungeon_entrance("Centrepiece space", 13409, 70, 500, new Item(8786, 1)),
		Dead_tree("Tree space", 13411, 5, 31, new Item(8417, 1)),
		Nice_tree("Tree space", 13412, 10, 44, new Item(8419, 1)),
		Oak_tree("Tree space", 13413, 15, 70, new Item(8421, 1)),
		Willow_tree("Tree space", 13414, 30, 100, new Item(8423, 1)),
		Maple_tree("Tree space", 13415, 45, 122, new Item(8425, 1)),
		Yew_tree("Tree space", 13416, 60, 141, new Item(8427, 1)),
		Magic_tree("Tree space", 13417, 75, 223, new Item(8429, 1)),
		Plant("Small Plant space 1", 13431, 1, 31, new Item(8431, 1)),
		Small_fern("Small Plant space 1", 13432, 6, 70, new Item(8433, 1)),
		Fern("Small Plant space 1", 13433, 12, 100, new Item(8435, 1)),
		Dock_leaf("Small Plant space 2", 13434, 1, 31, new Item(8431, 1)),
		Thistle("Small Plant space 2", 13435, 6, 70, new Item(8433, 1)),
		Reeds("Small Plant space 2", 13436, 12, 100, new Item(8435, 1)),
		Short_plant("Big Plant space 2", 13428, 1, 31, new Item(8431, 1)),
		Large_leaf_bush("Big Plant space 2", 13429, 6, 70, new Item(8433, 1)),
		Huge_plant("Big Plant space 2", 13430, 12, 100, new Item(8435, 1)),
		Big_fern("Big Plant space 1", 13425, 1, 31, new Item(8431, 1)),
		Bush("Big Plant space 1", 13426, 6, 70, new Item(8433, 1)),
		Tall_plant("Big Plant space 1", 13427, 12, 100, new Item(8435, 1));
		/* Chapel */
//		Guthix_Symbol("Chair space", OBJECTID, 48, 120, new Item(Oak_Planks, 2)),
//		Saradomin_Symbol("Chair space", OBJECTID, 48, 120, new Item(Oak_Planks, 2)),
//		Zamorak_Symbol("Chair space", OBJECTID, 48, 120, new Item(Oak_Planks, 2)),
//		Guthix_Icon("Chair space", OBJECTID, 59, 960, new Item(Teak_Planks, 4), new Item(Gold_leaves, 2)),
//		Saradomin_Icon("Chair space", OBJECTID, 59, 960, new Item(Teak_Planks, 4), new Item(Gold_leaves, 2)),
//		Zamorak_Icon("Chair space", OBJECTID, 59, 960, new Item(Teak_Planks, 4), new Item(Gold_leaves, 2)),
//		Icon_of_Bob("Chair space", OBJECTID, 71, 100, new Item(Mahogany_Planks, 4), new Item(Gold_Leaves, 2)),
//		Windchimes("Chair space", OBJECTID, 49, 323, new Item(Oak_Planks, 4), new Item(Nails, 4) ,new Item(Steel_Bar, 4)),
//		Bells("Chair space", OBJECTID, 58, 480, new Item(Teak_Planks, 4), new Item(Steel_Bar, 6)),
//		Organ("Chair space", OBJECTID, 69, 680, new Item(Mahogany_Plank, 4), new Item(Steel_Bar, 6)),
//		Small_Statue("Chair space", OBJECTID, 49, 40, new Item(Limestone_brick, 2)),
//		Medium_Statue("Chair space", OBJECTID, 69, 500, new Item(Marble_Block, 1)),
//		Large_Statue("Chair space", OBJECTID, 89, 1500, new Item(Marble_Block, 3)),
//		Shuttered_Window("Chair space", OBJECTID, 49, 228, new Item(Planks, 8), new Item(Nails, 8)),
//		Decorative_Window("Chair space", OBJECTID, 69, 4, new Item(Molten_glass, 8)),
//		Stained_Glass("Chair space", OBJECTID, 89, 5, new Item(Molten_glass, 16)),
//		Steel_torches("Chair space", OBJECTID, 45, 80, new Item(Steel_Bar, 2)),
//		Wooden_torches("Chair space", OBJECTID, 49, 58, new Item(Planks, 2), new Item(Nails, 2)),
//		Steel_Candlesticks("Chair space", OBJECTID, 124, 5, new Item(Steel_Bar, 16), new Item(Candles, 6)),
//		Gold_Candlesticks("Chair space", OBJECTID, 57, 46, new Item(Gold_Bar, 6), new Item(Candles, 6)),
//		Incense_Burners("Chair space", OBJECTID, 61, 280, new Item(Steel_Bar, 2), new Item(Oak_Plank, 4)),
//		Mahogany_Burners("Chair space", OBJECTID, 65, 600, new Item(Steel_Bar, 2), new Item(Mahog_Plank, 4)),
//		Marble_Burners("Chair space", OBJECTID, 69, 1600, new Item(Steel_Bar, 2), new Item(Marble_Block, 2)),
//		Oak_Altar("Chair space", OBJECTID, 45, 240, new Item(Oak_Planks, 4)),
//		Teak_Altar("Chair space", OBJECTID, 50, 360, new Item(Teak_Planks, 2)),
//		Cloth_Altar("Chair space", OBJECTID, 56, 390, new Item(Teak_Planks, 4), new Item(Bolts_of_cloth, 2)),
//		Mahogany_Altar("Chair space", OBJECTID, 60, 590, new Item(Mahogany_Planks, 4), new Item(Bolts_of_cloth, 2)),
//		Limestone_Altar("Chair space", OBJECTID, 64, 910, new Item(Mahogany_Planks, 6), new Item(Bolts_of_cloth, 2), new Item(limestone_bricks, 2)),
//		Marble_Altar("Chair space", OBJECTID, 70, 1030, new Item(Marble_Block, 2), new Item(Bolts_of_cloth, 2)),
//		Gilded_Altar("Chair space", OBJECTID, 75, 2230, new Item(Marble_Block, 2), new Item(Bolts_of_cloth, 2), new Item(Gold_leaves, 4)),
		/* Kitchen */
//		Beer_Barrel("Chair space", OBJECTID, 7, 87, new Item(Planks, 3), new Item(Nails, 3),
//		Cider_Barrel("Chair space", OBJECTID, 12, 91, new Item(Planks, 3), new Item(Nails, 3), new Item(Glass_of_Cider, 8),
//		Asgarnian_ale_Barrel("Chair space", OBJECTID, 18, 184, new Item(Oak_Plank, 3), new Item(Glass_of_Asgarnian_ale, 8),
//		Greenmans_Ale_Barrel("Chair space", OBJECTID, 26, 184, new Item(Oak_Plank, 3), new Item(Glass_Greenman's_Ale, 8),
//		Dragon_Bitter_Barrel("Chair space", OBJECTID, 36, 224, new Item(Oak_Plank, 3), new Item(Glass_of_Dragon_Bitter, 8), new Item(Steel_bar, 2),
//		Chefs_Delight_Barrel("Chair space", OBJECTID, 48, 224, new Item(Oak_Plank, 3), new Item(Glass_Of_Chef's_Delight, 8), new Item(Steel_bar, 2),
//		Wood_Kitchen_Table("Chair space", OBJECTID, 12, 87, new Item(Planks, 3), new Item(Nails, 3),
//		Oak_Kitchen_Table("Chair space", OBJECTID, 32, 180, new Item(Oak_Planks, 3),
//		Teak_Kitchen_Table("Chair space", OBJECTID, 52, 270, new Item(Teak_Planks, 3),
//		FirePit("Chair space", OBJECTID, 5, 40, new Item(Steel_Bar, 1), new Item(Soft_Clay, 2),
//		FirePit_With_hook("Chair space", OBJECTID, 11, 60, new Item(Steel_Bar, 2), new Item(Soft_Clay, 2),
//		FirePit_with_pot("Chair space", OBJECTID, 17, 80, new Item(Steel_Bar, 3), new Item(Soft_Clay, 2),
//		Small_Oven("Chair space", OBJECTID, 24, 80, new Item(Steel_Bar, 4),
//		Large_Oven("Chair space", OBJECTID, 29, 100, new Item(Steel_Bar, 5),
//		Steel_Range("Chair space", OBJECTID, 34, 120, new Item(Steel_Bar, 6),
//		Fancy_Range("Chair space", OBJECTID, 42, 160, new Item(Steel_Bar, 8),
//		Wooden_Larder("Chair space", OBJECTID, 9, 228, new Item(Plank, 8), new Item(Nails, 8),
//		Oak_Larder("Chair space", OBJECTID, 33, 480, new Item(Oak_Plank, 8),
//		Teak_Larder("Chair space", OBJECTID, 43, 750, new Item(Teak_Plank, 8), new Item(Bolt_Of_Cloth, 2),
//		Wooden_Shelves_1("Chair space", OBJECTID, 6, 87, new Item(Plank, 3), new Item(Nails, 3),
//		Wooden_Shelves_2("Chair space", OBJECTID, 12, 147, new Item(Plank, 3), new Item(Nails, 3), new Item(Soft_Clay, 6),
//		Wooden_Shelves_3("Chair space", OBJECTID, 23, 147, new Item(Plank, 3), new Item(Nails, 3), new Item(Soft_Clay, 6),
//		Oak_Shelves_1("Chair space", OBJECTID, 34, 240, new Item(Oak_Plank, 3), new Item(Soft_Clay, 6),
//		Oak_Shelves_2("Chair space", OBJECTID, 45, 240, new Item(Oak_Plank, 3), new Item(Soft_Clay, 6),
//		Teak_Shelves_1("Chair space", OBJECTID, 56, 330, new Item(Teak_Plank, 3), new Item(Soft_Clay, 6),
//		Teak_Shelves_2("Chair space", OBJECTID, 67, 930, new Item(Teak_Plank, 3), new Item(Gold_Leaves, 2), new Item(Soft_Clay, 6),
//		Pump_and_Drain("Chair space", OBJECTID, 7, 100, new Item(Steel_Bar, 5),
//		Pump_and_Tub("Chair space", OBJECTID, 27, 200, new Item(Steel_Bar, 10),
//		Sink("Chair space", OBJECTID, 47, 300, new Item(Steel_Bar, 15),
//		Cat_Blanket("Chair space", OBJECTID, 5, 15, new Item(Bolt_of_cloth, 1),
//		Cat_Basket("Chair space", OBJECTID, 19, 58, new Item(Plank, 2), new Item(Nails, 2),
//		Cushioned_Cat_Basket("Chair space", OBJECTID, 33, 58, new Item(Plank, 2), new Item(Nails, 2), new Item(Wool, 2),
		/* Throne Room */
//		Oak_Throne("Chair space", OBJECTID, 60, 800, new Item(Oak_Plank, 5), new Item(Marble_Block, 1)),
//		Teak_Throne("Chair space", OBJECTID, 67, 1450, new Item(Teak_Plank, 5), new Item(Marble_Block, 2)),
//		Mahogany_Throne("Chair space", OBJECTID, 74, 2200, new Item(Mahogany_Plank, 5), new Item(Marble_Block, 3)),
//		Gilded_Throne("Chair space", OBJECTID, 81, 1700, new Item(Mahogany_Plank, 5), new Item(Marble_Block, 2), new Item(Gold_Leaves, 3)),
//		Skeleton_Throne("Chair space", OBJECTID, 88, 7003, new Item(Magic_Stones, 5), new Item(Marble_Blocks, 4), new Item(Bones, 5), new Item(Skull, 2)),
//		Crystal_Throne("Chair space", OBJECTID, 95, 15000, new Item(Magic_Stones, 15)),
//		Demonic_Throne("Chair space", OBJECTID, 99, 25000, new Item(Magic_Stones, 25)),
//		Carved_Teak_Bench("Chair space", OBJECTID, 44, 360, new Item(Teak_Plank, 4)),
//		Mahogany_Bench("Chair space", OBJECTID, 52, 560, new Item(Mahogany_Plank, 4)),
//		Gilded_Bench("Chair space", OBJECTID, 61, 1760, new Item(Mahogany_Plank, 4), new Item(Gold_Leave, 4)),
//		Oak_decoration("Chair space", OBJECTID, 16, 120, new Item(Oak_Plank, 2)),
//		Teak_decoration("Chair space", OBJECTID, 36, 180, new Item(Teak_Plank, 2)),
//		Gilded_decoration("Chair space", OBJECTID, 56, 1020, new Item(Mahogany_Plank, 3), new Item(Gold_Leave, 2)),
//		Round_Shield("Chair space", OBJECTID, 66, 120, new Item(Oak_Plank, 2)),
//		Square_Shield("Chair space", OBJECTID, 76, 360, new Item(Teak_Plank, 4)),
//		Kite_Shield("Chair space", OBJECTID, 86, 420, new Item(Mahogany_Plank, 3)),
		/* Workspace */
//		Wooden_Workbench("Chair space", OBJECTID, 17, 143, new Item(Plank, 5)), new Item(Nail, 5)),
//		Oak_Workbench("Chair space", OBJECTID, 32, 300, new Item(Oak_Plank, 5)),
//		Steel_framed_Bench("Chair space", OBJECTID, 46, 440, new Item(Oak_Plank, 6), new Item(Steel_Bar, 4)),
//		Bench_With_Vice("Chair space", OBJECTID, 62, 140, new Item(Oak_Plank, 2), new Item(Steel_Bar, 1), new Item(Steel_Framed_Bench, 1)),
//		Bench_with_a_Lathe("Chair space", OBJECTID, 77, 140, new Item(Oak_Plank, 2), new Item(Steel_Bar, 1), new Item(Bench_With_Vice, 1)),
//		Pluming_STand("Chair space", OBJECTID, 16, 120, new Item(Oak_Plank, 2)),
//		Shield_Easel("Chair space", OBJECTID, 41, 240, new Item(Oak_Plank, 4)),
//		Banner_Easel("Chair space", OBJECTID, 66, 510, new Item(Oak_Plank, 8), new Item(Bolt_Of_Cloth, 2)),
//		Repair_Bench("Chair space", OBJECTID, 15, 120, new Item(Oak_Plank, 2)),
//		Whetstone("Chair space", OBJECTID, 35, 260, new Item(Oak_Plank, 4), new Item(limestone_Brick, 1)),
//		Armour_Stand("Chair space", OBJECTID, 55, 500, new Item(Oak_Plank, 8), new Item(limestone_Brick, 1)),
//		Tool_Store_1("Chair space", OBJECTID, 15, 120, new Item(Oak_Plank, 2)),
//		Tool_Store_2("Chair space", OBJECTID, 25, 120, new Item(Oak_Plank, 2)),
//		Tool_Store_3("Chair space", OBJECTID, 35, 120, new Item(Oak_Plank, 2)),
//		Tool_Store_4("Chair space", OBJECTID, 44, 120, new Item(Oak_Plank, 2)),
//		Tool_Store_5("Chair space", OBJECTID, 55, 121, new Item(Oak_Plank, 2)),
//		Crafting_Table_1("Chair space", OBJECTID, 16, 240, new Item(Oak_Plank, 4)),
//		Crafting_Table_2("Chair space", OBJECTID, 25, 1, new Item(Molten_Glass, 1)),
//		Crafting_Table_3("Chair space", OBJECTID, 34, 2, new Item(Molten_Glass, 2)),
//		Crafting_Table_4("Chair space", OBJECTID, 42, 120, new Item(Oak_Plank, 2)),
		
		private String hotSpot;
		private int objectId;
		private int levelRequired;
		private float xp;
		private Item[] itemsRequired;
		
		private static Map<Integer, FurnitureObject> realIds = new HashMap<Integer, FurnitureObject>();
		private static Map<String, FurnitureObject> hotspotName = new HashMap<String, FurnitureObject>();

		public static FurnitureObject forRealId(int realId) {
			return realIds.get(realId);
		}
		
		public static ArrayList<FurnitureObject> forHotSpotName(String shadowed) {
			ArrayList<FurnitureObject> furniture = new ArrayList<FurnitureObject>();
			for(FurnitureObject object : FurnitureObject.values()) {
				if (object.getHotSpotName().equals(shadowed))
					furniture.add(object);
			}
			return furniture;
		}

		static {
			for (FurnitureObject object : FurnitureObject.values()) {
				realIds.put(object.getRealId(), object);
			}
			for (FurnitureObject object : FurnitureObject.values()) {
				hotspotName.put(object.getHotSpotName(), object);
			}
		}
		
		private FurnitureObject(String hotSpot, int objectId, int levelRequired, float xp, Item... itemsRequired) {
			this.hotSpot = hotSpot;
			this.objectId = objectId;
			this.levelRequired = levelRequired;
			this.xp = xp;
			this.itemsRequired = itemsRequired;
		}
		
		public String getHotSpotName() {
			return hotSpot;
		}
		
		public int getRealId() {
			return objectId;
		}
		
		public int getLevelRequired() {
			return levelRequired;
		}
		
		public float getXp() {
			return xp;
		}
		
		public Item[] getItemsRequired() {
			return itemsRequired;
		}
	}
	
	public static void sitInChair(WorldObject o, Player player) {
		int emote = 0;
		switch (o.getId()) {
		case 13587:
			switch (o.getRotation()) {
			case 1:
				emote = 4088;
				break;
			case 4:
				emote = 4088;
				break;
			default:
				emote = 4087;
				break;
			}
			break;
		}
		WorldTile tile = new WorldTile(o.getX(), o.getY(), 0);
		player.setNextWorldTile(tile);
		player.setNextAnimation(new Animation(emote));
	}
	
	public static void lightFireplace(WorldObject o, Player player) {
		int oldX, oldY, oldFace, oldType, oldId, oldPlane;
		oldX = o.getX();
		oldY = o.getY();
		oldFace = o.getRotation();
		oldType = o.getType();
		oldId = o.getId();
		oldPlane = o.getPlane();
		if (player.getInventory().containsItems(new int[] {590, 1511}, new int[] {1, 1})) {
			World.spawnObject(new WorldObject(oldId + 1, oldType, oldFace, oldX, oldY, oldPlane), true);
		} else {
			player.getPackets().sendGameMessage("You need a tinderbox and logs to light the fireplace!");
		}
	}
	
	public static void addObject(Player player, WorldObject object) {
		player.getConObjectsToBeLoaded().add(object);
		World.spawnObject(object, true);
	}
	
	public static void removeObject(Player player, WorldObject object) {
		FurnitureObject furniture = FurnitureObject.forRealId(object.getId());
		if (furniture != null) {
			player.getConObjectsToBeLoaded().remove(object);
			World.spawnObject(new WorldObject(-1, object.getType(), object.getRotation(), object.getX(), object.getY(), object.getPlane()), false);
		}
	}
	
	public static void buildFurniture(WorldObject o, Player player) {
		WorldObject object = null;
//		int cornerX = (player.getBoundChuncks()[0] * 8) + (player.getRoomX() * 8); //Btm corner x
//		int cornerY = (player.getBoundChuncks()[1] * 8) + (player.getRoomY() * 8); //Btm corner y
		FurnitureObject furniture = FurnitureObject.forRealId(o.getId());
		if (furniture != null) {
			if (!player.getInventory().containsItems(furniture.getItemsRequired())) {
				player.getPackets().sendGameMessage("You do not have the items required to build that.");
				return;
			}
			if (player.getSkills().getLevel(Skills.CONSTRUCTION) < furniture.getLevelRequired()) {
				player.getPackets().sendGameMessage("You need a construction level of "+furniture.getLevelRequired()+" to build that.");
				return;
			}
			player.setNextAnimation(new Animation(898));
			object = new WorldObject(furniture.getRealId(), o.getType(), o.getRotation(), o.getX(), o.getY(), o.getPlane());
			addObject(player, object);
			player.getSkills().addXp(Skills.CONSTRUCTION, furniture.getXp());
		}
	}
	
//	private static WorldObject[] getObjectsInRoom(Player player) {
//		Region region = World.getRegion(RegionBuilder.getRegionHash(player.getBoundChuncks()[0]/8, player.getBoundChuncks()[1]/8));
//		int boundX = player.getRoomX() * 8;
//		int boundY = player.getRoomY() * 8;
//		for(int x = boundX; x < boundX+8; x++) {
//            for(int y = boundY; y < boundY+8; y++) {
//                WorldObject[] objects = region.getObjects(player.getPlane(), x, y);
//                if (objects != null) {
//                    return objects;
//                }
//            }
//		}
//		return null;
//	}
	
//	private static int convertX(int localX, Player player) {
//		return (localX + 8 * (player.getChunkX() - (102 >> 4)));
//	}
//	
//	private static int convertY(int localY, Player player) {
//		return (localY + 8 * (player.getChunkY() - (102 >> 4)));
//	}

	public static void refreshRoom(Player player) {
		house.refresh(player, player.getBoundChuncks());
	}

}
